﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.DBOper
{
    public class Cls_Channel
    {
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        private int sectionId;

        public int SectionId
        {
            get { return sectionId; }
            set { sectionId = value; }
        }

        private byte channelId;

        public byte ChannelId
        {
            get { return channelId; }
            set { channelId = value; }
        }
        private string channelName;

        public string ChannelName
        {
            get { return channelName; }
            set { channelName = value; }
        }

        private byte isUse;

        public byte IsUse
        {
            get { return isUse; }
            set { isUse = value; }
        }

        private string testPointName;

        public string TestPointName
        {
            get { return testPointName; }
            set { testPointName = value; }
        }

        private byte direction;

        public byte Direction
        {
            get { return direction; }
            set { direction = value; }
        }
        private byte sigType;

        public byte SigType
        {
            get { return sigType; }
            set { sigType = value; }
        }

        private byte sigUnit;

        public byte SigUnit
        {
            get { return sigUnit; }
            set { sigUnit = value; }
        }


        private float sensitivity;

        public float Sensitivity
        {
            get { return sensitivity; }
            set { sensitivity = value; }
        }
        private byte sensitivityUnit;

        public byte SensitivityUnit
        {
            get { return sensitivityUnit; }
            set { sensitivityUnit = value; }
        }


        private string sensorType;

        public string SensorType
        {
            get { return sensorType; }
            set { sensorType = value; }
        }

        private string sensorManufacturer;

        public string SensorManufacturer
        {
            get { return sensorManufacturer; }
            set { sensorManufacturer = value; }
        }

        private string sensorNo;

        public string SensorNo
        {
            get { return sensorNo; }
            set { sensorNo = value; }
        }

    }


    public class OperateChannel
    {
        /// <summary>
        /// 通过id查询通道配置信息
        /// </summary>
        /// <param name="testid">测试ID</param>
        /// <param name="filepath">数据库路径</param>
        public Cls_Channel GetChannelBySectionId(int SectionId,byte ChannelId)
        {
            Cls_Channel clschannel = null;
            string connString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0} ;Persist Security Info=False;Jet OLEDB:Database Password={1}", Mdl_Class.FilePath, Mdl_Class.AccessPassWord);
            using (MyDb_Access mydb = new MyDb_Access(connString))
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("select * from T_Channel  where SectionId={0} and ChannelId={1}", SectionId, ChannelId);
                    mydb.ExecuteReader(sql);
                    if (mydb.DataReader.Read())
                    {
                        clschannel = new Cls_Channel();
                        clschannel.Id = Convert.ToInt32(mydb.DataReader["ID"]);
                        clschannel.SectionId = Convert.ToInt32(mydb.DataReader["SectionId"].ToString());
                        clschannel.ChannelId = Convert.ToByte(mydb.DataReader["ChannelId"]);
                        clschannel.ChannelName = Convert.ToString(mydb.DataReader["ChannelName"]);
                        clschannel.IsUse = Convert.ToByte(mydb.DataReader["IsUse"]);
                        clschannel.TestPointName = Convert.ToString(mydb.DataReader["TestPointName"]);
                        clschannel.Direction = Convert.ToByte(mydb.DataReader["Direction"]);
                        clschannel.SigType = Convert.ToByte(mydb.DataReader["SigType"]);
                        clschannel.SigUnit = Convert.ToByte(mydb.DataReader["SigUnit"]);
                        clschannel.Sensitivity = Convert.ToSingle(mydb.DataReader["Sensitivity"]);
                        clschannel.SensitivityUnit = Convert.ToByte(mydb.DataReader["SensitivityUnit"]);
                        clschannel.SensorType = Convert.ToString(mydb.DataReader["SensorType"]);
                        clschannel.SensorManufacturer = Convert.ToString(mydb.DataReader["SensorManufacturer"]);
                        clschannel.SensorNo = Convert.ToString(mydb.DataReader["SensorNo"]);
                

                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    clschannel = null;
                    Mdl_Class.WriteLog.dbErrLog("GetChannelBySectionId(int SectionId,int ChannelId)", ex.ToString());
                }

                return clschannel;
            }
        }

        /// <summary>
        /// 查询所有通道配置信息
        /// </summary>
        public List<Cls_Channel> GetChannelList(int SectionId)
        {
            List<Cls_Channel> testlist = new List<Cls_Channel>();
            string connString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0} ;Persist Security Info=False;Jet OLEDB:Database Password={1}", Mdl_Class.FilePath, Mdl_Class.AccessPassWord);
            using (MyDb_Access mydb = new MyDb_Access(connString))
            {

                try
                {
                    mydb.Open();
                    string sql = "select * from T_Channel where SectionId=" + SectionId;

                    mydb.ExecuteReader(sql);
                    while (mydb.DataReader.Read())
                    {
                        Cls_Channel clschannel = new Cls_Channel();


                        clschannel.Id = Convert.ToInt32(mydb.DataReader["ID"]);
                        clschannel.SectionId = Convert.ToInt32(mydb.DataReader["SectionId"].ToString());
                        clschannel.ChannelId = Convert.ToByte(mydb.DataReader["ChannelId"]);
                        clschannel.ChannelName = Convert.ToString(mydb.DataReader["ChannelName"]);
                        clschannel.IsUse = Convert.ToByte(mydb.DataReader["IsUse"]);
                        clschannel.TestPointName = Convert.ToString(mydb.DataReader["TestPointName"]);
                        clschannel.Direction = Convert.ToByte(mydb.DataReader["Direction"]);
                        clschannel.SigType = Convert.ToByte(mydb.DataReader["SigType"]);
                        clschannel.SigUnit = Convert.ToByte(mydb.DataReader["SigUnit"]);
                        clschannel.Sensitivity = Convert.ToSingle(mydb.DataReader["Sensitivity"]);
                        clschannel.SensitivityUnit = Convert.ToByte(mydb.DataReader["SensitivityUnit"]);
                        clschannel.SensorType = Convert.ToString(mydb.DataReader["SensorType"]);
                        clschannel.SensorManufacturer = Convert.ToString(mydb.DataReader["SensorManufacturer"]);
                        clschannel.SensorNo = Convert.ToString(mydb.DataReader["SensorNo"]);

                        testlist.Add(clschannel);
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    testlist = null;
                    Mdl_Class.WriteLog.dbErrLog("GetTestList(string filepath)", ex.ToString());
                }
                return testlist;
            }
        }

        /// <summary>
        /// 添加通道配置信息
        /// </summary>
        /// <param name="clstest">测试类</param>
        /// <param name="filepath">数据库路径</param>
        public int AddCls_Channel(List<Cls_Channel> clstestlist, int SectionId)
        {
            int updatecount = 0;
            string connString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0} ;Persist Security Info=False;Jet OLEDB:Database Password={1}", Mdl_Class.FilePath, Mdl_Class.AccessPassWord);
            using (MyDb_Access mydb = new MyDb_Access(connString))
            {
                try
                {
                    mydb.Open();
                    mydb.BeginTransaction();
                    string sql_del = string.Format("delete from  T_Channel where SectionId={0}", SectionId);
                    updatecount = mydb.ExecuteNonQuery(sql_del);
                    if (updatecount >=0)
                    {
                        for (int i = 0; i < clstestlist.Count;i++ )
                        {
                            string sql_add = string.Format("insert into T_Channel(SectionId,ChannelId,ChannelName,IsUse,TestPointName,Direction,SigType,SigUnit,Sensitivity,SensitivityUnit,SensorType,SensorManufacturer,SensorNO) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}')",
                                clstestlist[i].SectionId,
                                clstestlist[i].ChannelId,
                                clstestlist[i].ChannelName,
                                clstestlist[i].IsUse,
                                clstestlist[i].TestPointName,
                                clstestlist[i].Direction,
                                clstestlist[i].SigType,
                                clstestlist[i].SigUnit,
                                clstestlist[i].Sensitivity,
                                clstestlist[i].SensitivityUnit,
                                clstestlist[i].SensorType,
                                clstestlist[i].SensorManufacturer,
                                clstestlist[i].SensorNo);
                            updatecount = mydb.ExecuteNonQuery(sql_add);
                            if (updatecount<0)
                            {
                                break;
                            }
                        }

                    }
                    if (updatecount >= 0)
                    {
                        mydb.Commit();

                    }
                    else 
                    {
                        mydb.Rollback();
                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    updatecount = -1;
                    Mdl_Class.WriteLog.dbErrLog("AddTest(Cls_Test clstest,string filepath)", ex.ToString());
                }
            }
            return updatecount;
        }



     

    }

}
